--{api="guestbook",params={{key="contents",value=content},{key="contents",value=content}},

local HttpMgr = class("HttpMgr");
function HttpMgr:ctor()
    self:init();
end

function HttpMgr:init(entry)
    self._listenerList = {};
    self.signkey = '96e79218965eb72c92a549dd5a330112'
    --self.serverAddr = "192.168.1.134:8080";
    --self.serverAddr = "114.67.229.15:8080";
    self.serverAddr = "47.100.184.180:8080";
end

function HttpMgr:getServerAddress(entry)
    return "http://" .. self.serverAddr .. "/" .. entry .. "?method=";
end


function HttpMgr:get(args)
    local cbk = args.cbk;
    local function requestGetCbk(event)
        local requestBack = event.request;
        if event.name ~= "completed" then
            return; 
        end
        local code = requestBack:getResponseStatusCode()
        if code ~= 200 then
            print("code ", code)
            return; 
        end
        local resp = requestBack:getResponseString()
        if cbk then
            cbk(resp);
        end
    end
    
    local param = json.encode(args.params or {})
    local sign = crypto.md5(args.api .. "&" .. param .. "&" .. self.signkey)
    local url = self:getServerAddress(args.entry) .. args.api .. "&sign=" .. sign .. "&param=" .. string.urlencode(param);
    mgrs.log:printDebug("url = %s",url)
    local request = network.createHTTPRequest(requestGetCbk,url,"GET");
    request:setTimeout(30);
    request:start();
end


function HttpMgr:post(args)
    local cbk = args.cbk;
    --local url = self._httpServerAddr .. "?action=" .. args.api;
    local param = json.encode(args.params or {})
    local sign = crypto.md5(args.api .. "&" .. param .. "&" .. self.signkey)
    local url = self:getServerAddress(args.entry) .. args.api .. "&sign=" .. sign;
    local params = args.params or {};
    local function httpPostCbk(event)
        local requestBack = event.request;
        if event.name ~= "completed" then
            return; 
        end
        local code = requestBack:getResponseStatusCode()
        if code ~= 200 then
            print("code ", code)
            return; 
        end
        local resp = requestBack:getResponseString()
        if cbk then
            cbk(resp);
        end
    end;
    local request = network.createHTTPRequest(httpPostCbk,url,"POST");
    mgrs.log:printDump(params,"post params");
    for key, var in pairs(params) do
        request:addPOSTValue(var.key, var.value);
    end
    request:start();
end


function HttpMgr:registerMsg(msgid,callBack)
    self._listenerList[msgid] = self._listenerList[msgid] or {};
    table.insert(self._listenerList[msgid],callBack);
end

-- �Ƴ�ע����Ϣ
function HttpMgr:removeRegister( msgid,callBack )
    if self._listenerList[msgid] then
        for k,v in pairs(self._listenerList[msgid]) do
            if v == callBack then
                table.remove(self._listenerList[msgid],k);
                break;
            end
        end
    end
end

function HttpMgr:disPatchProto(msgid,data)
    if self._listenerList[msgid] and #self._listenerList[msgid] ~= 0 then
        for k,v in pairs(self._listenerList[msgid]) do
            v(data);
        end
    else
        mgrs.log:printNotice("no lisenter liscen %d",msgid);
    end
end

function HttpMgr:uploadFile(serverUrl,filePath,contentType)
	network.uploadFile(
	function(evt)
        if evt.name == "completed" then
            local request = evt.request
            mgrs.log:printNotice("REQUEST getResponseStatusCode() = %d", request:getResponseStatusCode())
            mgrs.log:printNotice("REQUEST getResponseHeadersString() =\n%s", request:getResponseHeadersString())
            mgrs.log:printNotice("REQUEST getResponseDataLength() = %d", request:getResponseDataLength())
            mgrs.log:printNotice("REQUEST getResponseString() =\n%s", request:getResponseString())
        end
    end,
    serverUrl,
    {
        fileFieldName = "filepath",
        filePath = filePath,
        contentType = contentType,
        extra={{"act", "upload"}}
    })
end


mgrs.http = nil;
mgrs.singleClass(mgrs,HttpMgr,"http");
--endregion
